package patientimport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.util.CollectionUtils;
import org.testng.annotations.Test;

import com.alibaba.fastjson.JSON;
import com.bbcare.comm.Constants;
import com.bbcare.followup.plat.entity.Patient;
import com.bbcare.followup.plat.service.IFamilyReportService;
import com.bbcare.followup.plat.store.ibatis.IFamilyReportDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientDAO;

@ContextConfiguration(locations = { "classpath*:/ibatis-config.xml",
		"classpath*:/spring-mvc.xml","classpath*:/spring-mybatis.xml"
		})  
public class SynchGrowthResult extends AbstractTestNGSpringContextTests {

  @Autowired
  IPatientDAO patientdao ;
  
  @Autowired
  IFamilyReportDAO familyReportDAO ;
  
  @Autowired
  IFamilyReportService familyReportService;
  
  //pt_growth_record result actualResult 字段数据更新
  @Test
  public void service() throws Exception {
	  List<Map<String,Object>> list= familyReportDAO.selectAllGrowthRecords(new HashMap<String,Object>());
	  if(!CollectionUtils.isEmpty(list)){
		  Map<String,Object> tempMap = null;
		  for(int i = 0 ;i<list.size();i++){
			  try{
				  tempMap = list.get(i);
				  Map<String,List<Map<String,Object>>> result = new HashMap<String,List<Map<String,Object>>>();
				  Map<String,List<Map<String,Object>>> actualResult = new HashMap<String,List<Map<String,Object>>>();
				  Map<String,Object> map = new HashMap<String,Object>();
					map.put("tenantId",tempMap.get("tenantId"));
					map.put("userId",tempMap.get("userId"));
					map.put("testTime",tempMap.get("testTime").toString());
					
					Patient patient = patientdao.selectById(map);
					if(null!= tempMap.get("weight") && null !=patient){
						map.put("values",tempMap.get("weight"));
						map.put("type",Constants.DEVELOPPROJECT_WEIGHT);
						Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						Map<String, Object> zscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						
						Map<String,Object> rsMap = new HashMap<String,Object>();
						if(percentiles!=null && !percentiles.isEmpty()){
							rsMap.put("percentileRange",percentiles.get("percentiles"));
							rsMap.put("time",percentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",zscore.get("zscore"));
							rsMap.put("time",zscore.get("time"));
						}
						
						List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							listMap.add(rsMap);
							result.put("weightResult", listMap);
						}
						
						Map<String, Object> actualPercentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						Map<String, Object> actualZscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						rsMap = new HashMap<String,Object>();
						if(actualPercentiles!=null && !actualPercentiles.isEmpty()){
							rsMap.put("percentileRange",actualPercentiles.get("percentiles"));
							rsMap.put("time",actualPercentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",actualZscore.get("zscore"));
							rsMap.put("time",actualZscore.get("time"));
						}
						List<Map<String,Object>> actualList = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							actualList.add(rsMap);
							actualResult.put("weightResult", actualList);
						}
					}
					
					if(null!= tempMap.get("hight") && null !=patient){
						map.put("values",tempMap.get("hight"));
						map.put("type",Constants.DEVELOPPROJECT_LENGTH);
						Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						Map<String, Object> zscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						
						Map<String,Object> rsMap = new HashMap<String,Object>();
						if(percentiles!=null && !percentiles.isEmpty()){
							rsMap.put("percentileRange",percentiles.get("percentiles"));
							rsMap.put("time",percentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",zscore.get("zscore"));
							rsMap.put("time",zscore.get("time"));
						}
						
						List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							listMap.add(rsMap);
							result.put("heightResult", listMap);
						}
						
						Map<String, Object> actualPercentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						Map<String, Object> actualZscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						rsMap = new HashMap<String,Object>();
						if(actualPercentiles!=null && !actualPercentiles.isEmpty()){
							rsMap.put("percentileRange",actualPercentiles.get("percentiles"));
							rsMap.put("time",actualPercentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",actualZscore.get("zscore"));
							rsMap.put("time",actualZscore.get("time"));
						}
						
						List<Map<String,Object>> actualList = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							actualList.add(rsMap);
							actualResult.put("heightResult", actualList);
						}
					}
					
					if(null!= tempMap.get("headCircumference") && null !=patient){
						map.put("values",tempMap.get("headCircumference"));
						map.put("type",Constants.DEVELOPPROJECT_HEAD);
						Map<String, Object> percentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						Map<String, Object> zscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_CORRECT,patient);
						
						Map<String,Object> rsMap = new HashMap<String,Object>();
						if(percentiles!=null && !percentiles.isEmpty()){
							rsMap.put("percentileRange",percentiles.get("percentiles"));
							rsMap.put("time",percentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",zscore.get("zscore"));
							rsMap.put("time",zscore.get("time"));
						}
						
						List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							listMap.add(rsMap);
							result.put("headtResult", listMap);
						}
						
						Map<String, Object> actualPercentiles = familyReportService.getGrowthPercentiles(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						Map<String, Object> actualZscore = familyReportService.getGrowthZscore(map,Constants.GROWTH_AGE_TYPE_ACTUAL,patient);
						rsMap = new HashMap<String,Object>();
						if(actualPercentiles!=null && !actualPercentiles.isEmpty()){
							rsMap.put("percentileRange",actualPercentiles.get("percentiles"));
							rsMap.put("time",actualPercentiles.get("time"));
						}
						if(zscore!=null && !zscore.isEmpty()){
							rsMap.put("zcorerange",actualZscore.get("zscore"));
							rsMap.put("time",actualZscore.get("time"));
						}
						List<Map<String,Object>> actualList = new ArrayList<Map<String,Object>>();
						if(!rsMap.isEmpty()){
							actualList.add(rsMap);
							actualResult.put("headtResult", actualList);
						}
					}
					
					Map<String,Object> param = new HashMap<String,Object>();
					if(!CollectionUtils.isEmpty(result)){
						param.put("result",JSON.toJSONString(result));
					}
					if(!CollectionUtils.isEmpty(actualResult)){
						param.put("actualResult",JSON.toJSONString(actualResult));
					}
					param.put("id",tempMap.get("growthRecordId"));
					familyReportDAO.updateGrowthRecord(param);  
			  }catch(Exception e){
				  e.printStackTrace();
				  System.out.println("null______userId="+tempMap.get("userId"));
			  }
		  }
	  }
  }
}
  